package com.woniuxy.mapper;





import java.util.List;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

import org.apache.ibatis.annotations.Select;

import com.woniuxy.pojo.Guest;
import com.woniuxy.pojo.Guest;
import com.woniuxy.pojo.Opinon;

import com.woniuxy.pojo.Guest;
import com.woniuxy.pojo.Opinon;

import org.apache.ibatis.annotations.Insert;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import com.woniuxy.pojo.Order;
import com.woniuxy.pojo.Order2;
import com.woniuxy.pojo.OrderDetail;
import com.woniuxy.pojo.OrderDetailgjf;
import com.woniuxy.pojo.OrderDetail;
import com.woniuxy.pojo.OrderDetaillyq;
import com.woniuxy.pojo.Order_dgy;
import com.woniuxy.pojo.Ordergjf;
import com.woniuxy.pojo.Room;
import com.woniuxy.pojo.Roomgjf;


public interface OrderMapper {

	@Select("select room.rtid,imgpath,rtname,rprice,rfood,rspring FROM room,roomimg,roomtype where roomimg.rtid=roomtype.rtid AND room.rtid=#{rtid} LIMIT 1")
	Roomgjf findroom(int rtid);
	
	@Select("select oid,onumber,ostate,DATE_FORMAT(ostart, '%Y-%c-%d %H:%i:%S'),ocancel,opay,opaynumber,totalprice,opledge,autocancel,uid,mid from `order` where uid=#{uid}")
	public ArrayList<Order> all(int uid);
	
	@Select("select oid,onumber,ostate,ostart,ocancel,opay,opaynumber,totalprice,opledge,autocancel,uid,mid from `order`")
	public ArrayList<Order2> all2();
	
		@Select("select odid,rtid,oldprice,newprice,dayamount,DATE_FORMAT(checkin, '%Y-%c-%e') from orderdetail where oid=#{oid}")
		@Results({
			@Result(id=true, column="odid",property="odid"),
			@Result(column="rtid", property="imgpath",
			one=@One(select="com.woniuxy.mapper.RoomMapper.findRoomHeadImg")
			),
			@Result(column="rtid", property="rtname",
			one=@One(select="com.woniuxy.mapper.RoomMapper.fingRtName")
			),
			@Result(column="oldprice", property="oldprice"),
			@Result(column="newprice", property="newprice"),
			@Result(column="dayamount", property="dayamount"),
			@Result(column="DATE_FORMAT(checkin, '%Y-%c-%e')", property="checkin")
		})
		public ArrayList<OrderDetaillyq> findByOid(int oid);

	@Select("select gid,gname,gidcard FROM user,guest where user.uid=guest.uid AND user.uid=#{uid}")
	List<Guest> findGuset(int uid);
	
	@Update("UPDATE orderdetail SET grade=#{grade}, opinion=#{opinion} WHERE odid=#{odid}")
	int saveOpinion(int odid, Float grade, String opinion);

	@Update("UPDATE `order` SET ostate=#{ostate} where oid = (select oid from orderdetail where odid=#{odid})")
	int updateState(int odid,String ostate);

	@Select("SELECT rtname,DATE_FORMAT(checkin, '%Y-%c-%e') as checkin,grade,opinion from orderdetail INNER JOIN roomtype ON orderdetail.rtid=roomtype.rtid "
			+ "WHERE grade !=''")
	ArrayList<Opinon> allOpinion();
	
	@Select("SELECT percent FROM `user`,member,discount WHERE `user`.mid=member.mid and member.mtype=discount.dname and uid=#{uid}")
	float findMember(int uid);
	
	@Select("SELECT percent FROM discount WHERE dname='全场折扣'")
	float findDiscount1();
	
	@Select("SELECT percent FROM discount WHERE dname='线上折扣'")
	float findDiscount2();
	
	@Insert("INSERT INTO `order`(onumber,ostart,ostate,opay,uid,totalprice) VALUES(#{onumber},#{ostart},#{ostate},#{opay},#{uid},#{totalprice})")
	int creOrder(Order order);
	
	@Select("SELECT oid FROM `order` WHERE onumber=#{onumber}")
	int findOidByOnumber(String onumber);
	


	@Insert("INSERT INTO orderdetail(oldprice,newprice,checkin,checkout,dayamount,arrival,message,rtid,gid,oid) VALUES(#{oldprice},#{newprice},#{checkin},#{checkout},#{dayamount},#{arrival},#{message},#{rtid},#{gid},#{oid})")
	int cl(OrderDetailgjf ol);

	@Update("UPDATE `order` SET ostate=#{ostate} where oid=#{oid}")
	void changeStateByOid(int oid, String ostate);

	@Update("UPDATE orderdetail SET dele=#{dele} where oid=#{oid}")
	void changeDeleByOid(int oid, int dele);

	@Select("SELECT rtname,DATE_FORMAT(checkin, '%Y-%c-%e') as checkin,grade,opinion from orderdetail INNER JOIN roomtype ON orderdetail.rtid=roomtype.rtid "
			+ "WHERE grade>3.5")
	ArrayList<Opinon> allOpinion2();
	
	@Select("SELECT rtname,DATE_FORMAT(checkin, '%Y-%c-%e') as checkin,grade,opinion from orderdetail INNER JOIN roomtype ON orderdetail.rtid=roomtype.rtid "
			+ "WHERE grade<=3.5 and grade>2")
	ArrayList<Opinon> allOpinion3();
	
	@Select("SELECT rtname,DATE_FORMAT(checkin, '%Y-%c-%e') as checkin,grade,opinion from orderdetail INNER JOIN roomtype ON orderdetail.rtid=roomtype.rtid "
			+ "WHERE grade<=2")
	ArrayList<Opinon> allOpinion4();

	
	//添加订单
	@Insert("insert into `order`(onumber,ostate,ostart,opay,totalprice,opledge,mid) values(#{onumber},#{ostate},#{ostart},#{opay},#{totalprice},#{opledge},#{mid})")
	public int addOrder(Order_dgy order);

	//查找订单
	@Select("select * from `order` where onumber = #{onumber}")
	public Order_dgy findByOrder(Order_dgy order);

	//修改订单
	@Update("update `order` set ostate = #{ostate} where oid=#{oid}")
	public int changeOrder(Order_dgy order);

	@Select("select * from `order`")
	@Results({
		@Result(id=true,column="oid",property="oid"),
		@Result(column="onumber",property="onumber"),
		@Result(column="oid",property="orderDetails",
			many=@Many(
					select="com.woniuxy.mapper.OrderDetailMapper.findByOid"
				)),
		@Result(column="ostate",property="ostate"),
		@Result(column="ostart",property="ostart"),
		@Result(column="opay",property="opay"),
		@Result(column="totalprice",property="totalprice")
	})
	public List<Order_dgy> findAll();

	@Select("select * from `order` where onumber=#{message}")
	@Results({
		@Result(id=true,column="oid",property="oid"),
		@Result(column="onumber",property="onumber"),
		@Result(column="oid",property="orderDetails",
			many=@Many(
					select="com.woniuxy.mapper.OrderDetailMapper.findByOid"
				)),
		@Result(column="ostate",property="ostate"),
		@Result(column="ostart",property="ostart"),
		@Result(column="opay",property="opay"),
		@Result(column="totalprice",property="totalprice")
	})
	public Order_dgy findOrderByMessage(String message);

	@Select("select * from `order` where oid=#{oid}")
	@Results({
		@Result(id=true,column="oid",property="oid"),
		@Result(column="onumber",property="onumber"),
		@Result(column="oid",property="orderDetails",
			many=@Many(
					select="com.woniuxy.mapper.OrderDetailMapper.findByOid"
				)),
		@Result(column="ostate",property="ostate"),
		@Result(column="ostart",property="ostart"),
		@Result(column="opay",property="opay"),
		@Result(column="totalprice",property="totalprice")
	})
	public Order_dgy findById(String oid);

	@Select("select * from `order` where ostate='已消费'")
	public List<Order_dgy> completeOrder();

	@Select("select * from `order` where ostate='已取消'")
	public List<Order_dgy> cancelOrder();

	//删除订单
	@Update("update `order` set state='已取消' where onumber=#{onumber}")
	public int deleteOrder(String oNumber);


}

